package com.example.treedemo;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import bean.StrData;
import bean.TreeData;
import bean.TreeNode;
import view.Node;
import view.NodeResource;
import view.TreeListView;

public class MainActivity extends Activity {

    private TreeListView listView;
    private Button button;
    private Button btn_delect;
    ArrayList<TreeNode> topNodes = new ArrayList<TreeNode>();
    ArrayList<TreeNode> allNodes = new ArrayList<TreeNode>();
    private TextView text_select;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        btn_delect = (Button) findViewById(R.id.btn_delect);
        text_select = (TextView) findViewById(R.id.text_select);
        RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.main_relative_layout);


        listView = new TreeListView(this, initNodeTree());
        relativeLayout.addView(listView);
        //获取数据
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<Node> list = (ArrayList<Node>) listView.get();
                list.toString();
                if (listView.get().size() == 0) {
                    return;
                }
                String s = "";
                for (int i = 0; i < listView.get().size(); i++) {
                    s += listView.get().get(i).getTitle() + "  ";
                }
                text_select.setText(s);
                Log.i("====list", listView.get() + "");
            }
        });
        btn_delect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listView.setUnSelect();
            }
        });





      //  String booking = TestResult.getFromAssets(TestResult.booking, this);







    }

    public List<NodeResource> initNodeTree() {
//        List<NodeResource> list = new ArrayList<NodeResource>();
//        NodeResource n1 = new NodeResource("" + -1, "17018m170diosfvieqh5drn2u30mi1shn5", "智设施测试", "123");
//        list.add(n1);
//        NodeResource n2 = new NodeResource("17018m170diosfvieqh5drn2u30mi1shn5", "17072n261s99viv1piif7pvshj0h3mkke2", "a部门", "123");
//        list.add(n2);
//        NodeResource n3 = new NodeResource("17072n261s99viv1piif7pvshj0h3mkke2", "17072p144434ru15iuhbgrlk0jc05qecj7", "a111", "123");
//        list.add(n3);
//        NodeResource n4 = new NodeResource("17072n261s99viv1piif7pvshj0h3mkke2", "17072p13qjnce5pvocgaupj7qu8qkfgk75", "a222", "123");
//        list.add(n4);
//        NodeResource n5 = new NodeResource("17018m170diosfvieqh5drn2u30mi1shn5", "17072n27rkh63ieb9ggtpqirv8d1j1oh6k", "b部门", "123");
//        list.add(n5);

    //    getData();





        String booking = TestResult.getFromAssets(TestResult.booking, this);
        Gson gson = new Gson();
        StrData   strData = gson.fromJson(booking, StrData.class);


        TreeData treeData=new TreeData();


        treeData.setChildren(strData.getData());

        initTreeNodesList(treeData);








        ArrayList<NodeResource> listTree = new ArrayList<>();
//
        for (int i = 0; i < allNodes.size(); i++) {
            NodeResource nr = new NodeResource(allNodes.get(i).getParendId(),
                    allNodes.get(i).getId(),
                    allNodes.get(i).getContentText(), "123");
            listTree.add(nr);
        }
        return listTree;
    }

    public ArrayList getData() {

      //  String str = TestData.data;

        Log.i("-----allNodes", allNodes.size() + "");
        return allNodes;

    }

    private void initTreeNodesList(TreeData roomInfo) {

        if ("-1".equals(roomInfo.getRoomid())) {
            if (roomInfo.getChildren() == null) {
                return;
            }

            for (int i = 0; i < roomInfo.getChildren().size(); i++) {
                //添加节点  -- 节点名称，节点level，节点id，父节点id，是否有子节点，是否展开
                TreeNode rootTreeNode = new TreeNode(roomInfo.getChildren().get(i).getRoomname(),
                        TreeNode.TOP_LEVEL,
                        roomInfo.getChildren().get(i).getRoomid(), TreeNode.NO_PARENT, true, false);
                topNodes.add(rootTreeNode);
                allNodes.add(rootTreeNode);
            }
        } else {
            for (int i = 0; i < allNodes.size(); i++) {
                if (roomInfo.getFroomid().equals(allNodes.get(i).getId())) {
                    TreeNode treeNode = new TreeNode(roomInfo.getRoomname(),
                            allNodes.get(i).getLevel() + 1, roomInfo.getRoomid(), roomInfo.getFroomid(),
                            !"true".equals(roomInfo.getLeaf())
                            , false);
                    allNodes.add(treeNode);
                }
            }
        }

        if (!"true".equals(roomInfo.getLeaf())) {
            for (TreeData info : roomInfo.getChildren()) {
                initTreeNodesList(info);
            }
        }

    }
}
